﻿using System;

namespace Graph
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请选择你想要计算面积的图形形状：");
            Console.WriteLine("1.长方形 2.正方形 3.三角形");
            string name = Console.ReadLine();
            Graph graph = Factory.CreateFunction(name);
            if (graph != null)
            {
                Console.WriteLine("所求图形面积为：" + graph.Area());

            }
            else
            Console.WriteLine("系统找不到该图形");
            Console.ReadKey();

        }
    }
    abstract class Graph
    {
        //计算面积
        public abstract double Area();
        //判断图形是否合理
        public abstract void IsReasonable();

    }
    //长方形
    class Rectangle:Graph
    {
        double height = 0;
        double width = 0;
        public Rectangle()
        {
            IsReasonable();
        }
        public override double Area()
        {
            return height * width;
        }
        public override void IsReasonable()
        {
            while (true)
            {
                Console.WriteLine("请输入长方形的长");
                height = int.Parse(Console.ReadLine());
                Console.WriteLine("请输入长方形的宽");
                width = int.Parse(Console.ReadLine());
                if (height == 0|| width ==0)
                {
                    Console.WriteLine("请输入非零数据！");
                    continue;
                }
                break;
            }
        }

    }
    //正方形
    class Square:Graph
    {
        double width=0;
        public Square()
        {
            IsReasonable();
        }
        public override double Area()
        {
            return width*width;
        }
        public override void IsReasonable()
        {
            while (true)
            {
                Console.WriteLine("请输入正方形的边长：");
                width = int.Parse(Console.ReadLine());
                if (width == 0)
                {
                    Console.WriteLine("请输入非零数据：");
                    continue;
                }
                break;
            }
        }
    }
    //三角形
    class Triangle : Graph
    {
        double side1, side2, side3 = 0;
        
        public Triangle()
        {
            IsReasonable();
        }
        public override double Area()
        {
            double p = side1 + side2 + side3;
            double S = Math.Sqrt(p * (p - side1) * (p - side2) * (p - side3));
            return S;
        }
        public override void IsReasonable()
        {
            while (true)
            {
                Console.WriteLine("请输入三角形的第一个边长");
                side1 = double.Parse(Console.ReadLine());
                Console.WriteLine("请输入三角形的第二个边长");
                side2 = double.Parse(Console.ReadLine());
                Console.WriteLine("请输入三角形的第三个边长");
                side3 = double.Parse(Console.ReadLine());
                if (side1 + side2 <= side3 || side2 + side3 <= side1 || side1 + side3 <= side2)
                {
                    Console.WriteLine("该三角形不可理！");
                    continue;
                }
                break;
            }
        }
    }
    //工厂
    class Factory
    {
        public static Graph CreateFunction(string name)
        {
            switch (name)
            {
                case "1":return new Rectangle();
                case "2":return new Square();
                case "3":return new Triangle();
                default:
                    return null;
            }
        }
    }
}
